﻿using FPSO.Models.Domain;
using OpcUaHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FPSO.HMI.Utils
{
    /// <summary>
    /// 随机数工具类
    /// </summary>
    internal class CommUtil
    {
        private static readonly Random random = new Random();
        /// <summary>
        /// 生成指定范围的随机整数
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>随机整数</returns>
        /// <exception cref="ArgumentException"></exception>
        public static int GetIntNumber(int min, int max)
        {
            if (min >= max)
            {
                throw new ArgumentException("范围的最小值必须小于最大值。");
            }

            using (var rng = RandomNumberGenerator.Create())
            {
                byte[] data = new byte[4]; // 4 字节用于生成一个整数
                rng.GetBytes(data);

                int randomNumber = BitConverter.ToInt32(data, 0);

                return Math.Abs(randomNumber % (max - min)) + min;
            }
        }

        /// <summary>
        /// 生成指定范围和小数点位数的随机数
        /// </summary>
        /// <param name="minValue">最小值</param>
        /// <param name="maxValue">最大值</param>
        /// <param name="decimalPlaces">小数点后位数</param>
        /// <returns></returns>
        public static double GetDoubleNumber(double minValue, double maxValue, int decimalPlaces)
        {
            double randomValue = random.NextDouble();
            double mappedValue = minValue + (randomValue * (maxValue - minValue));
            double roundedValue = Math.Round(mappedValue, decimalPlaces);
            return roundedValue;
        }

		//通讯报警信息
		public static readonly List<string> CommStatusAlarmList = new List<string>
		{
			"OPC服务器连接失败",
			"数据写入OPC服务器失败",
			"读取OPC服务器数据失败",
			"液位遥测系统通讯连接异常",
			"阀门遥控系统通讯连接异常",
			"货油系统通讯连接异常",
		};

		//船舶状态预警信息
		public static readonly List<string> ShipStatusPrealarmList = new List<string>
		{
			"OPC服务器连接失败",
			"数据写入OPC服务器失败",
			"读取OPC服务器数据失败",
		};

		//船舶状态报警信息
		public static readonly List<string> ShipStatusAlarmList = new List<string>
		{
			"OPC服务器连接失败",
			"数据写入OPC服务器失败",
			"读取OPC服务器数据失败",
		};

		//舱室状态预警信息
		public static readonly List<string> TankStatusPrealarmList = new List<string>
		{
			"OPC服务器连接失败",
			"数据写入OPC服务器失败",
			"读取OPC服务器数据失败",
		};

		//舱室状态报警信息
		public static readonly List<string> TankStatusAlarmList = new List<string>
		{
			"OPC服务器连接失败",
			"数据写入OPC服务器失败",
			"读取OPC服务器数据失败",
		};

		//设备状态异常
		public static readonly List<string> ItemStatusAlarmList = new List<string>
		{
			"阀门状态异常",
			"液位遥测读取数据异常",
			"泵状态异常",
			"控制阀状态异常",
		};

		//用户操作信息 6
		public static readonly List<string> OperationList = new List<string>
		{
			"下发压载舱倒舱指令",
			"下发压载舱调平指令",
		};

		//系统提示信息 7
		public static readonly List<string> SystemTipList = new List<string>
		{
			"加载任务配置信息OpcTask失败",
			"新增调载任务失败",
			"已存在指定压载舱任务",
			"加载监测点配置信息失败OpcTag失败",
			"backup1",
			"backup1",
			"backup1",
			"backup1",
			"backup1",
			"backup1",
			"打开数据库失败", //10
			"读取数据库信息失败",
			"写入数据库信息失败",
			"更新数据库信息失败",
			"backup1",
			"backup1",
			"backup1",
			"backup1",
			"backup1",
			"backup1",
			"输入格式不符合要求",//20
		};

		//根据信息类型和信息拼接具体信息string，type为信息类型，tag为特定类型下的信息条目，input为具体信息
		private static string GetSystemInfo(int type, int tag, string input)
		{
			string pre = string.Empty;
			switch(type)
			{
				case 0:
					pre = CommStatusAlarmList[tag];
					break; 
				case 1:
					pre = ShipStatusPrealarmList[tag];
					break;
				case 2:
					pre = ShipStatusAlarmList[tag];
					break;
				case 3:
					pre = TankStatusPrealarmList[tag];
					break;
				case 4:
					pre = TankStatusAlarmList[tag];
					break;
				case 5:
					pre = ItemStatusAlarmList[tag];
					break;
				case 6:
					pre = OperationList[tag];
					break;
				case 7:
					pre = SystemTipList[tag];
					break;
			}
			if (pre != string.Empty)
			{
				string rtn = string.Format("{0}:{1}", pre, input);
				return rtn;
			}
			return string.Empty;
		}

		//增加systemlog
		//type=0,通讯报警信息，type=1,船舶状态预警信息，type=2,船舶状态报警信息，
		//type=3,舱室状态预警信息，type=4,舱室状态报警信息，type=5,设备状态报警信息，type=6,用户操作信息，type=7,系统提示信息，
		public static void AddSystemLog(int type, int tag, string input)
		{
			string log = GetSystemInfo(type, tag, input);
			SystemLog logout = new SystemLog();
			logout.LogName = log;
			logout.LogType = type;
			SysConfig.g_SystemLogService.AddSystemLog(logout);
		}
	}
}
